package com.lenovo.smarttraffic.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.RouteInfoBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Caleb.F
 * on 2019-11-23
 * at 20:01
 */
public class MapDetailRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final Integer EDGE_VIEW = 0;
    private static final Integer RECYCLER_VIEW = 1;
    private static final String TAG="MapDetailRecycler";
    private Context mContext;
    private RouteInfoBean mRouteInfoBean;
    private List<RouteInfoBean.RouteBean.PathsBean.StepsBean> steps;

    public MapDetailRecyclerAdapter(Context context, RouteInfoBean routeInfoBean) {
        mContext = context;
        mRouteInfoBean = routeInfoBean;
        handlerData();
        Log.d(TAG, "onBindViewHolder: "+steps.size());
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == EDGE_VIEW) {
            return new EdgeViewHolder(LayoutInflater.from(mContext).inflate(R.layout.recycler_edge_view, parent, false));
        } else {
            Log.d(TAG, "onBindViewHolder: "+"eeeeeee11111");
            return new RecyclerItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.map_recycler_view, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof EdgeViewHolder){
            if(holder.getLayoutPosition()==0){
                ((EdgeViewHolder) holder).mImageView16.setImageResource(R.mipmap.start);
                ((EdgeViewHolder) holder).mTvRoad.setText(mRouteInfoBean.getStartSpot());
                ((EdgeViewHolder) holder).mTvInstration.setText("起点");
                ((EdgeViewHolder) holder).mborderView.setVisibility(View.VISIBLE);
            }
            else{
                ((EdgeViewHolder) holder).mImageView16.setImageResource(R.mipmap.end);
                ((EdgeViewHolder) holder).mTvRoad.setText(mRouteInfoBean.getEndSpot());
                ((EdgeViewHolder) holder).mTvInstration.setText("终点");
                ((EdgeViewHolder) holder).mborderView.setVisibility(View.GONE);
            }
        }
        else if(holder instanceof RecyclerItemViewHolder){
            ((RecyclerItemViewHolder) holder).mTvInstration.setText(steps.get(position-1).getInstruction());
            if(steps.get(position-1).getAction() instanceof String )
           ((RecyclerItemViewHolder) holder).mTvAction.setText((CharSequence) steps.get(position-1).getAction());
            else{
                ((RecyclerItemViewHolder) holder).mTvAction.setText("");
            }
            if(steps.get(position-1).getRoad() instanceof String )
                ((RecyclerItemViewHolder) holder).mTvRoad.setText((CharSequence) steps.get(position-1).getRoad());
            else{
                ((RecyclerItemViewHolder) holder).mTvRoad.setText("");
            }

        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 || position==steps.size()+1) {
            return EDGE_VIEW;
        } else {
            return RECYCLER_VIEW;
        }
    }

    private void handlerData() {
        List<RouteInfoBean.RouteBean.PathsBean> paths = mRouteInfoBean.getRoute().getPaths();
        steps=new ArrayList<>();
        for (RouteInfoBean.RouteBean.PathsBean item :
                paths) {
            steps.addAll(item.getSteps());
        }
    }

    @Override
    public int getItemCount() {
        return steps.size()+2;
    }

    class EdgeViewHolder extends RecyclerView.ViewHolder {

        TextView mTvAction;
        ImageView mImageView16;
        TextView mTvRoad;
        TextView mTvInstration;
        View mborderView;

        public EdgeViewHolder(View convertView) {
            super(convertView);
            mTvAction = convertView.findViewById(R.id.tv_action);
            mImageView16 = convertView.findViewById(R.id.imageView16);
            mTvRoad = convertView.findViewById(R.id.tv_road);
            mTvInstration = convertView.findViewById(R.id.tv_instration);
            mborderView=convertView.findViewById(R.id.divider);
        }
    }

    class RecyclerItemViewHolder extends RecyclerView.ViewHolder {
        TextView mTvAction;
        TextView mTvRoad;
        TextView mTvInstration;

        public RecyclerItemViewHolder(View convertView) {
            super(convertView);
            mTvAction = convertView.findViewById(R.id.tv_action);
            mTvRoad = convertView.findViewById(R.id.tv_road);
            mTvInstration = convertView.findViewById(R.id.tv_instration);
        }
    }



}
